package com.sgb.kjwl.view.widget.packers;

import android.content.Context;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.exoplayer2.decoder.OutputBuffer;
import com.sgb.kjwl.model.entity.PickDeviceEntity2;
import com.sgb.kjwl.model.entity.PickSelectEntity2;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.YoungAdapterOnItemClick;
import com.sgb.kjwl.view.ui.adapter.packers.HaveHookAdapter;
import com.sgb.kjwl.view.ui.adapter.packers.HaveHookNoTypeAdapter;
import com.sgb.kjwl.view.ui.adapter.packers.NoBodyAdapter;
import com.sgb.kjwl.view.ui.adapter.packers.NoHookAdapter;
import com.sgb.kjwl.view.ui.adapter.packers.NoHookNoTypeAdapter;
import com.swgk.core.dialog.PopViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : younghow
 * @date : 2021/9/23 10:29
 * description:
 */
public class TwoLevelPopLinkWindow<D extends PickSelectEntity2> extends ThreeLevelPopWindow<D, NoHookNoTypeAdapter<D>, HaveHookNoTypeAdapter<D>, NoBodyAdapter<D>>{

    private NoHookNoTypeAdapter<D> mProvinceAdapter;
    private HaveHookNoTypeAdapter<D> mCityAdapter;
    private D mProvince;
    private D mCity;
    private List<D> mProvinceList;
    private List<D> mCityList;
    private List<List<D>> mCityData;
    private SelectResultClick<D> mSelectResultClick;
    private String mName;
    private D mLastProvince;

    public TwoLevelPopLinkWindow(Context context) {
        super(context);
    }

    public void setSelectResultClick(SelectResultClick<D> mSelectResultClick) {
        this.mSelectResultClick = mSelectResultClick;
    }

    public void setTwoLevelData(List<D> provinceData, List<List<D>> cityData) {
        mProvinceAdapter.setDatas(provinceData);
        mProvinceAdapter.setDefItem(-1);

        mCityAdapter.setDatas(cityData.get(0));
        mCityAdapter.setDefItem(-1);

        resetItems(provinceData,cityData);
    }

    public void setTwoLevelData(List<D> provinceData, List<List<D>> cityData,int position) {
        mProvinceAdapter.setDatas(provinceData);
        mProvinceAdapter.setDefItem(-1);

        mCityAdapter.setDatas(cityData.get(position));
        mCityAdapter.setDefItem(-1);
        mCityData = cityData;
        providerProvinceView().scrollToPosition(position);

        resetItems(provinceData,cityData);
    }

    private void resetItems(List<D> provinceData, List<List<D>> cityData) {
        if (mProvinceAdapter != null){
            mProvinceAdapter.setItemSelect(true);
            mProvinceAdapter.setDatas(provinceData);
            setProvinceDefaultItem(0);
            mProvinceAdapter.setOnItemClick(new YoungAdapterOnItemClick<D>() {
                @Override
                public void onItemClick(D d, int position) {
                    mCityAdapter.setCheckDefultDatas(new ArrayList<>(),new SparseBooleanArray());

                    if (mProvince != null){
                        mProvince.setNo(d.getNo());
                        mProvince.setName(d.getName());
                    }
                    if (mCity != null){
                        mCity.setNo("");
                        mCity.setName("全部");
                    }
                    mCityAdapter.setDatas(cityData.get(position));
                    setCityDefaultItem(0);
                }
            });
        }

        if (mCityAdapter != null) {
            mCityAdapter.setItemSelect(true);
            mCityAdapter.setDatas(cityData.get(0));
            mCityAdapter.setDefItem(-1);
            setCityDefaultItem(0);
            mCityAdapter.setOnItemClick(new YoungAdapterOnItemClick<D>() {
                @Override
                public void onItemClick(D d, int position) {
                    if (mCity!=null){
                        mCity.setName(d.getName());
                        mCity.setNo(d.getNo());
                    }
                    setName(d.getNo());
                }
            });
        }
    }

    @Override
    protected void initView(PopViewHolder holder) {
        super.initView(holder);
        setAreaViewVisible(false);
        mCityList = new ArrayList<>();
        mProvinceList = new ArrayList<>();
    }

    @Override
    protected NoHookNoTypeAdapter<D> createProvinceAdapter() {
        mProvinceAdapter = new NoHookNoTypeAdapter<>(mContext,null);
        return mProvinceAdapter;
    }

    @Override
    protected HaveHookNoTypeAdapter<D> createCityAdapter() {
        mCityAdapter = new HaveHookNoTypeAdapter<>(mContext,null);
        return mCityAdapter;
    }

    @Override
    protected NoBodyAdapter<D> createAreaAdapter() {
        return new NoBodyAdapter<>(mContext,null);
    }

    @Override
    protected RecyclerView.LayoutManager createProvinceLayoutManage() {
        return new LinearLayoutManager(mContext);
    }

    @Override
    protected RecyclerView.LayoutManager createCityLayoutManage() {
        return new LinearLayoutManager(mContext);
    }

    @Override
    protected RecyclerView.LayoutManager createAreaLayoutManage() {
        return new LinearLayoutManager(mContext);
    }

    @Override
    protected void createOkMethod() {
        mProvinceList.clear();
        mCityList.clear();
        if (mLastProvince != null){
            mLastProvince.setNo(mProvince.getNo());
            mLastProvince.setName(mProvince.getName());
        }

        if (!mProvinceList.contains(mProvince)){
            mProvinceList.add(mProvince);
        }

        if (!mCityList.contains(mCity)){
            mCityList.add(mCity);
        }

        if (mSelectResultClick != null){
            mSelectResultClick.result(mProvinceList,mCityList,null);
        }
    }

    @Override
    protected void createCancelMethod() {
        if (mProvince != null){
            mProvince.setName("全部");
            mProvince.setNo("");
        }

        if (mCity != null){
            mCity.setName("全部");
            mCity.setNo("");
        }

        if (mLastProvince != null){
            mLastProvince.setName("全部");
            mLastProvince.setNo("");
        }

        if (mSelectResultClick != null){
            mSelectResultClick.reset();
        }
    }

    public void clear(){
        setCityDefaultItem(0);
        setProvinceDefaultItem(0);
        mProvinceList.clear();
        mCityList.clear();
    }

    public void setName(String name){
        this.mName = name;
    }

    public void setName(String name,String no){
        this.mName = name;
        if (mCityData != null){
            for (D data : mCityAdapter.getDatas()) {
                if (data.getName().equals(name)){
                    if (mCity == null) {
                        mCity = (D) new AddressEntity();
                    }
                    mCity.setName(data.getName());
                    mCity.setNo(data.getNo());
                }
            }
        }
    }

    public void setCity(D city){
        mCity = city;
    }

    public void setProvince(D d){
        mProvince = (D) new AddressEntity();
        mProvince.setName(d.getName());
        mProvince.setNo(d.getNo());
        mLastProvince = (D) new AddressEntity();
        mLastProvince.setName(d.getName());
        mLastProvince.setNo(d.getNo());
        mProvinceList.add(mProvince);
    }

    @Override
    public void showPopwindow(View view, int x, int y) {
        super.showPopwindow(view, x, y);
        if (mLastProvince != null){
            int position = setProvinceDefaultItemForNo(mLastProvince.getNo());
            if (position != -1){
                mCityAdapter.setDatas(mCityData.get(position));
            }
        }
    }
}
